package com.itheima.health.service.impl;

import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.prefs.BackingStoreException;

@DubboService
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleDao roleDao;

    //角色分页查询
    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        Object name = queryPageBean.getQueryString();

        //查询总条数
        Long total = roleDao.findCountRole();
        //每页显示条数
        Integer pageSize = queryPageBean.getPageSize();
        //查询页数
        Integer currentPage = queryPageBean.getCurrentPage();
        //查询起始条数
        Integer pageIndex = (currentPage - 1) * pageSize;
        List<Role> rows = roleDao.findPage(pageIndex, pageSize, name);

        return new PageResult(total, rows);
    }

    //新增 菜单管理回显
    @Override
    public List<Menu> findAllMenu() {
        return roleDao.findAllMenu();
    }

    //新增 权限管理回显
    @Override
    public List<Permission> findAllPermission() {
        return roleDao.findAllPermission();
    }

    //新增 操作
    @Override
    public void add(Integer[] permissions, Integer[] menus, Role role) {
        roleDao.addRole(role);
        System.out.println("新增回显id=" + role.getId());
        Integer id = role.getId();
        for (Integer permissionId : permissions) {
            roleDao.addpermission(id, permissionId);
        }
        for (Integer menuId : menus) {
            roleDao.addmenu(id, menuId);
        }
    }

    //删除
    @Override
    public void delete(Integer id) {
        //查询是否有关联数据  menu
        Integer CountFromMenu = roleDao.findCountByRoleId(id);
        if (CountFromMenu > 0) {
            throw new BusinessRuntimeException("当前角色与菜单管理有关联，不能删除,请先解除关联!");
        }
        //查询是否有关联数据  permission
        Integer permissionCountByRoleId = roleDao.findPermissionCountByRoleId(id);
        if (permissionCountByRoleId > 0) {
            throw new BusinessRuntimeException("当前角色与权限管理有关联，不能删除,请先解除关联!");
        }
        roleDao.deleteById(id);

    }

    //编辑 角色回显
    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findMenusByRoleId(Integer id) {
        return roleDao.findMenusByRoleId(id);
    }

    @Override
    public List<Integer> findPermissionsByRoleId(Integer id) {
        return roleDao.findPermissionsByRoleId(id);
    }
//编辑
    @Override
    public void edit(Role role, Integer[] permissions, Integer[] menus) {
        //编辑角色
        roleDao.editRole(role);
        //先删除关联 再新增
        Integer id = role.getId();
        roleDao.deletePermissionByRoleId(id);
        for (Integer permission : permissions) {
            roleDao.addpermission(id,permission);
        }

        roleDao.deleteMenusByRoleId(id);
        for (Integer menu : menus) {
            roleDao.addmenu(id,menu);
        }


    }


    /**
     * @Author: Administrator
     * @Date: 12:36 2021/1/19
     * @Parms []
     * @ReturnType: java.util.List<com.itheima.health.pojo.Role>
     * @Description: 查询所有角色信息
    */
    @Override
    public List<Role> findAll() {
        List<Role> list=roleDao.selectAll();
        return list;
    }
}
